<?php
/**
 * Clase para validar los datos del usuario
 * 
 * Se encarga de realizar la validadcion de cada uno de los datos enviados por un formularia
 * ya sea via POST o GET, es de vital importancia para no producir basura y evitar ataques
 * @author Alberto Berroteran (Ashrey)
 */
class ABValidation{
	protected $a_rules;
	protected $a_valrules;
	protected $a_error;
	protected $s_field;
	protected $s_alias;
	protected $a_ferror = array();
/**

 */
	function __construct($s_method = 'post'){
		$this->s_method = $s_method;
	}

	function add(){
		$a_args = func_get_args();
		$s_field = array_shift($a_args);
		$s_alias = $s_field;
		$a_field = explode(':', $s_field);
		if(count($a_field)>1){
			$s_field = $a_field[0];
			$s_alias = $a_field[1];
		}
		$a_tpm = array();
		foreach($a_args as $s_rule){
			$a_param = explode(':', $s_rule);
			$o_tmp = new stdClass();
			$o_tmp->filter = array_shift($a_param);
			$o_tmp->alias = $s_alias;
			$o_tmp->params = $a_param;
			$a_tmp[] = $o_tmp;
		}
		$this->a_rules[$s_field] = $a_tmp;
	}



	function doValidation(){
		//Recorre todos las reglas
		if(empty($this->a_rules))return true;
		$s_var = $_SERVER['REQUEST_METHOD'] == 'POST'? $_POST:$_GET;
		//lista campos
		foreach($this->a_rules as $s_field => $a_rules){
			//lista de reglas
			$this->s_field = $s_field;
			foreach($a_rules as $o_rules){
				$this->s_alias = $o_rules->alias;
				call_user_func_array(array($this, $o_rules->filter), $o_rules->params);
			}
		}
		/*
				}elseif($s_rule == 'numeric' and !is_numeric($s_value)){
					$this->addError($s_fname, $a_vrule[0]);
					continue;
				}elseif($s_rule == 'integer' and !ABValidation::isInt($s_value)){
					$this->addError($s_fname, $a_vrule[0]);
					continue;
				}elseif($s_rule == 'in' and !in_array($s_value, $a_vrule[1])){
					$this->addError($s_fname, $a_vrule[0]);
					continue;
				}elseif($s_rule == 'equal' and $s_value != $a_vrule[1]){
					$this->addError($s_fname, $a_vrule[0]);
					continue;
				}elseif($s_rule == 'custom' and !preg_match($a_vrule[1], $s_value)){
					$this->addError($s_fname, $a_vrule[0]);
					continue;
				}
			}
		}*/
	}
	
	public function addError($s_errorname){
		$this->a_ferror[] = $this->s_field;
		$this->a_error[$this->s_alias] = $s_errorname;
	}

	public function getErrors(){
		return empty($this->a_error) ? array(): $this->a_error;
	}
	
	public function getFields(){
		return $this->a_ferror;
	}
	
	/**
	 * Protected validation funcion
	 */
	protected function required($s_msg = 'This field is required!'){
		if(!isset($_POST[$this->s_field]) || empty($_POST[$this->s_field]))
			$this->addError($s_msg);
	}
	
	protected function url($s_msg = 'This Field should be a URL'){
		if(!preg_match('/^(https?|ftp)\:\/\/.*$/i', $_POST[$this->s_field]))
			$this->addError($s_msg);
	}
	
	protected function email($s_msg = 'Write a email valid'){
		if(!preg_match('/[a-z0-9]{3,}\@[a-z0-9]{3,}\.[a-z0-9]{2,4}/i', $_POST[$this->s_field]))
			$this->addError($s_msg);
	}

	protected function max($i_max, $s_msg = 'This Field is too long'){
		if(strlen($_POST[$this->s_field])>$i_max)
			$this->addError($s_msg);
	}

	protected function min($i_max, $s_msg = 'This Field is too short'){
		if(strlen($_POST[$this->s_field])<$i_max)
			$this->addError($s_msg);
	}

	protected function numeric($s_msg = 'This Field sholuld Numeric'){
		if(!preg_match('/[0-9a-f]{1,}/i', $this->s_field))
			$this->addError($s_msg);
	}


	protected function alphanumeric($s_msg = 'This Field sholuld Alphanueric'){
		if(!preg_match('/^[\w_0-9,\. ñáéíóúüÑÁÉÍÓÚÜ]*$/iu', $this->s_field))
			$this->addError($s_msg);
	}
}
?>
